package com.atguigu.edu.contorller;

import com.atguigu.edu.bean.PaperExamStats;
import com.atguigu.edu.service.PaperExamStatsService;
import com.atguigu.edu.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PaperExamStatsController {
    @Autowired
    private PaperExamStatsService paperExamStatsService;

    @RequestMapping("/paper")
    public String edu(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<PaperExamStats> paperList = paperExamStatsService.selectPaperExam(date);

        StringBuilder json = new StringBuilder("{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": {");
        json.append("\"total\": " + paperList.size() + ",");
        json.append("   \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"试卷名称\",\n" +
                "        \"id\": \"paper_title\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"试卷考试人数\",\n" +
                "        \"id\": \"testExamPeople\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"试卷平均分\",\n" +
                "        \"id\": \"testAvgScore\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"试卷平均时长\",\n" +
                "        \"id\": \"testAvgTime\"\n" +
                "      }\n" +
                "    ], \"superHeaders\": \n" +
                "      [\n" +
                "        {\n" +
                "          \"name\": \"粒度\",\n" +
                "          \"colspan\": 2\n" +
                "        },\n" +
                "        {\n" +
                "          \"name\": \"统计\",\n" +
                "          \"colspan\": 2\n" +
                "        }\n" +
                "      ]\n" +
                "    ,\"rows\": [");

        for (int i = 0; i < paperList.size(); i++) {
            PaperExamStats paperExamStats = paperList.get(i);
            json.append("      {\n" +
                    "        \"paper_title\": \"" + paperExamStats.getPaper_title() + "\",\n" +
                    "        \"testExamPeople\": \"" + paperExamStats.getTestExamPeople() + "\",\n" +
                    "        \"testAvgScore\": \"" + paperExamStats.getTestAvgScore() + "\",\n" +
                    "        \"testAvgTime\": " + paperExamStats.getTestAvgTime() + "\n" +
                    "      }");
            if (i < paperList.size() - 1) {
                json.append(",");
            }
        }
        json.append("]}}");
        return json.toString();
    }
}